import { assign } from 'min-dash'

export default function ContextPadProvider(
  config,
  injector,
  contextPad,
  modeling,
  elementFactory,
  connect,
  create,
  translate
) {
  this.modeling = modeling
  this.elementFactory = elementFactory
  this.connect = connect
  this.create = create
  this.translate = translate
  config = config || {}

  if (config.autoPlace !== false) {
    this.autoPlace = injector.get('autoPlace', false)
  }

  contextPad.registerProvider(this)
}

ContextPadProvider.$inject = [
  'config.contextPad',
  'injector',
  'contextPad',
  'modeling',
  'elementFactory',
  'connect',
  'create',
  'translate'
]

ContextPadProvider.prototype.getContextPadEntries = function(element) {
  const {
    autoPlace,
    create,
    elementFactory,
    translate,
    modeling,
    connect
  } = this

  function appendAction(type, className, title, options) {
    function appendStart(event, element) {
      var shape = elementFactory.createShape(assign({ type: type }, options))
      create.start(event, shape, {
        source: element
      })
    }

    var append = autoPlace
      ? function(event, element) {
        var shape = elementFactory.createShape(
          assign({ type: type }, options)
        )
        autoPlace.append(element, shape)
      }
      : appendStart

    return {
      group: 'model',
      className: className,
      title: title,
      action: {
        dragstart: appendStart,
        click: append
      }
    }
  }

  function removeElement(e) {
    modeling.removeElements([element])
  }
  function startConnect(event, element) {
    connect.start(event, element)
  }

  var actions = {}

  // if (
  //   element.type === 'bpmn:UserTask' ||
  //   element.type === 'bpmn:SequenceFlow'
  // ) {
  //
  // }
  if (element.type !== 'bpmn:EndEvent' && element.type !== 'bpmn:TextAnnotation') {
    assign(actions, {
      'append.end-event': appendAction(
        'bpmn:EndEvent',
        'bpmn-icon-end-event-none',
        translate('创建结束节点')
      ),
      'append.gateway': appendAction(
        'bpmn:ParallelGateway',
        'bpmn-icon-gateway-parallel',
        translate('创建并行网关')
      ),
      'append.user-task': appendAction(
        'bpmn:UserTask',
        'bpmn-icon-user-task',
        translate('创建用户任务')
      ),
      'append.text-annotation': appendAction(
        'bpmn:TextAnnotation',
        'bpmn-icon-text-annotation',
        translate('添加备注')
      ),
      'connect': {
        group: 'connect',
        className: 'bpmn-icon-connection-multi',
        title: translate('Connect using DataInputAssociation'),
        action: {
          click: startConnect,
          dragstart: startConnect
        }
      }
    })
  }
  assign(actions, {
    delete: {
      group: 'edit',
      className: 'bpmn-icon-trash',
      title: translate('Remove'),
      action: {
        click: removeElement
      }
    }
  })

  return actions
}
